﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class DataGroupsMan : xIx.Web.UI.PageBase
{
    public string SelNode
    {
        get
        {
            object v = ViewState["SelNode"];
            if (null != v)
            {
                return (string)v;
            }
            else
            {
                return default(string);
            }
        }
        set
        {
            ViewState["SelNode"] = value;
            //Label1.Text = value;
        }
    }
    public string SelID
    {
        get
        {
            return TreeView1.SelectedValue;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SelNode = string.Empty;
         
            TreeView1.Nodes.Clear();
            TreeNode r = new TreeNode();
            r.Text = "网站"; r.Value = string.Empty;
            TreeView1.Nodes.Add(r);
            r.Select();
            ReloadTree();
            TreeView1_SelectedNodeChanged(TreeView1,e);
        }
    }
    protected void ResetButton_Click(object sender, ImageClickEventArgs e)
    {
        ReloadGrid();
    }
    protected void SaveButton_Click(object sender, ImageClickEventArgs e)
    {
        if(Utils.CheckBizLocker("Sys_Groups"))
        {
            Alert("暂时无法操作，请稍后重试。");
            return;
        }
        Utils.SetBizLocker("Sys_Groups",true);
        try
        {
            using (xIx.Data.DbHelper dh = new xIx.Data.DbHelper())
            {
                DataTable dt = null;

                dt = dh.Table["Sys_Groups"].Fields("Max(ID) mid").Select();

                int nid = 1000;

                if (dt.Rows.Count > 0) { int.TryParse(dt.Rows[0][0].ToString(), out nid); }

                nid++;


                int ec = 0;
                foreach (GridViewRow gr in GridView1.Rows)
                {
                    try
                    {
                        if (gr.RowType == DataControlRowType.DataRow)
                        {
                            string rid = GridView1.DataKeys[gr.RowIndex].Value.ToString();
                            if ((gr.Cells[2].FindControl("Del") as CheckBox).Checked)
                            {
                                dh.Table["Sys_Groups"].Where("Path Like @Path").Delete(SelNode+"/"+rid+"/%");
                                dh.Table["Sys_Groups"].Where("ID=@RID").Delete(rid);
                            }
                            else
                            {
                                if ((gr.Cells[1].FindControl("Save") as CheckBox).Checked)
                                {
                                    //gr.Cells[1].FindControl("Save") as CheckBox).Checked=false;
                                    bool bn = string.IsNullOrEmpty(rid);
                                    string vt, vi, vim, vm;
                                    double vs = 1;
                                    vt = (gr.Cells[3].FindControl("Title") as TextBox).Text;
                                    vi = (gr.Cells[4].FindControl("Icon") as UserControls_ImgSelector).Value;
                                    vim = (gr.Cells[5].FindControl("Img") as UserControls_ImgSelector).Value;
                                    vm = (gr.Cells[6].FindControl("Memo") as TextBox).Text;
                                    double.TryParse((gr.Cells[7].FindControl("SortIdx") as TextBox).Text, out vs);
                                    if (string.IsNullOrEmpty(vt)) continue;
                                    if (!bn)
                                    {
                                        dh.Table["Sys_Groups"]
                                            .Fields("Title", "Icon", "Img", "Memo", "SortIdx")
                                            .UpdateWith(vt, vi, vim, vm, vs).Where("ID=@RID").Exec(rid);
                                    }
                                    else
                                    {
                                        dh.Table["Sys_Groups"]
                                            .Fields("ID", "Path","Pid", "Title", "Icon", "Img", "Memo", "SortIdx")
                                            .InsertWith(nid.ToString(), SelNode + "/" + nid.ToString(),SelID, vt, vi, vim, vm, vs).Exec(rid);
                                        nid++;
                                    }
                                }
                            }

                        }
                    }
                    catch
                    {
                        ec++;
                    }
                }
                if (ec > 0)
                {
                    Alert("保存时遇到错误。");
                }
            }
        }
        finally {
            Utils.SetBizLocker("Sys_Groups",false);
        }
        ReloadTree();
    }

    protected void ReloadGrid() {
        DataTable dt = null;
        using (xIx.Data.DbHelper dh = new xIx.Data.DbHelper())
        {
            dt = dh.Table["Sys_Groups"].Where("Pid=@SID").OrderBy("SortIdx Desc").Select(SelID);
        }
        for (int i = 0; i < 8; i++)
        {
            dt.Rows.Add(dt.NewRow());
            
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void ReloadTree()
    {

        TreeNode r = TreeView1.FindNode(SelNode);
        if (r == null) {
            r = TreeView1.Nodes[0];
            SelNode = r.ValuePath;
        }
        r.Select();
        r.ChildNodes.Clear();

        DataTable dt = null;
        using (xIx.Data.DbHelper dh = new xIx.Data.DbHelper())
        {
            dt = dh.Table["Sys_Groups"].Fields("ID","Title","Pid","SortIdx").OrderBy("Pid,SortIdx Desc").Select();
        }
        BindTreeSubNodes(r, dt);
        r.ExpandAll();

        ReloadGrid();
    }

    protected void BindTreeSubNodes(TreeNode r,DataTable dt) {
        foreach (DataRow dr in dt.Select("Pid='"+r.Value+"'","SortIdx Desc"))
        {
            TreeNode n = new TreeNode();
            n.Text=dr["Title"].ToString();n.Value=dr["ID"].ToString();
            r.ChildNodes.Add(n);
            BindTreeSubNodes(n, dt);
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        ReloadTree();

    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {

        SelNode = TreeView1.SelectedNode.ValuePath;
        Label3.Text = TreeView1.SelectedNode.Text + "[" + TreeView1.SelectedNode.ValuePath + "]";
        ReloadGrid();
    }
}
